angular.module('imsApp').controller('realTimeDataController', ['$scope', '$state', 'ImsAlert','AdDataManager',
    function ($scope, $state, ImsAlert,AdDataManager) {
        $scope.mode=1;
        var yTodayNewUser = [];
        var yestodayNewUser = [];
        var todayActiveUsers = [];
        var yesActiveUsers = [];
        var todayplayNum = [];
        var yesplayNum = [];

        var todayCumulativeAdUsers=[];
        var yesCumulativeAdUsers=[];

        var xAxis = [];

        AdDataManager.userDataRt().then(function (result) {
            var today=result.data.today;
            var yestoday=result.data.yestoday;
           $scope.userDataRtTotal=result.data.userDataRtTotalVO;
            today.forEach(function (item) {
                yTodayNewUser.push(item.newUsers);
                todayActiveUsers.push(item.activeUsers)
                todayplayNum.push(item.playNum)
                todayCumulativeAdUsers.push(item.activeUsers)

            })

            yestoday.forEach(function (item) {
                xAxis.push(item.hour+"点")
                yestodayNewUser.push(item.newUsers);
                yesActiveUsers.push(item.activeUsers);
                yesplayNum.push(item.playNum)
                yesCumulativeAdUsers.push(item.activeUsers)

            })

            $scope.filterClick(1);

        })




        $scope.filterClick=function (index) {
            console.log(index);
            $scope.mode=index;
            if(index==1){
                setDataShow(xAxis,yTodayNewUser,yestodayNewUser)
            }
            if(index==2){
                setDataShow(xAxis,todayActiveUsers,yesActiveUsers)
            }
            if(index==3){
                setDataShow(xAxis,todayplayNum,yesplayNum)
            }
            if (index == 4) {
                setDataShow(xAxis, todayCumulativeAdUsers, yesCumulativeAdUsers)
            }


        }


        function setDataShow(xAxis,yToday,yYestoday) {

            $scope.chartOpt = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data:['今日','昨日'],
                    bottom:"5"
                },

                toolbox: {
                    show: true,
                    feature: {
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: xAxis,
                        axisLabel: {
                            interval: 0,
                            rotate: 10
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '今日',
                        type:'line',
                        data: yToday,
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    },
                    {
                        name: '昨日',
                        type:'line',
                        data: yYestoday,
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };

        }

    }])


